package com.jnu.keepaccounts.data;

import android.content.Context;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;

public class DataBank {

    private final Context context;
    ArrayList<Data> datas=new ArrayList<>();
    ArrayList<DataTou> dataTous= new ArrayList<>();
    ArrayList<DataTiaoMu> dataTiaoMus = new ArrayList<>();
    ArrayList<DataZhuanZhang> dataZhuanZhangs = new ArrayList<>();
//    private String DATA_FILE_NAME = "data";
    private String DATA_TIAO_MU_FILE_NAME= "datatiaomu";
    private String DATA_TOU_FILE_NAME = "datatou";
    private String DATA_ZHUANZHANG_FILE ="zhuanzhang";

    public DataBank(Context context){
        this.context = context;
    }

//    public ArrayList<Data> loadData() {
//        try {
//            ObjectInputStream objectInputStream = new ObjectInputStream(context.openFileInput(DATA_FILE_NAME));
//            datas = (ArrayList<Data>) objectInputStream.readObject();
//            Log.d("databank","load done");
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//        return datas;
//    }
//
//    public void saveData(){
//        ObjectOutputStream objectOutputStream = null;
//        try {
//            objectOutputStream = new ObjectOutputStream(context.openFileOutput(DATA_FILE_NAME,Context.MODE_APPEND));
//            objectOutputStream.writeObject(datas);
//            Log.d("data bank","save done");
//        }  catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                if (objectOutputStream !=null){
//                    objectOutputStream.close();
//                }
//            }catch (IOException e){
//                e.printStackTrace();
//            }
//        }
//    }


    public ArrayList<DataZhuanZhang> loadZData(){
        ObjectInputStream objectInputStream;
        try {
            objectInputStream = new ObjectInputStream(context.openFileInput(DATA_ZHUANZHANG_FILE));
            dataZhuanZhangs = (ArrayList<DataZhuanZhang>) objectInputStream.readObject();
            objectInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return dataZhuanZhangs;
    }

    public void saveZData(){
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(context.openFileOutput(DATA_ZHUANZHANG_FILE,Context.MODE_PRIVATE));
            objectOutputStream.writeObject(dataZhuanZhangs);
        }  catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (objectOutputStream !=null){
                    objectOutputStream.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    public ArrayList<Data> loadData(){

        dataTous = new ArrayList<>();
        dataTiaoMus = new ArrayList<>();
        datas = new ArrayList<>();
        ObjectInputStream objectInputStream;
        try {
            objectInputStream = new ObjectInputStream(context.openFileInput(DATA_TIAO_MU_FILE_NAME));
            dataTiaoMus = (ArrayList<DataTiaoMu>) objectInputStream.readObject();
            objectInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            objectInputStream = new ObjectInputStream(context.openFileInput(DATA_TOU_FILE_NAME));
            dataTous = (ArrayList<DataTou>) objectInputStream.readObject();
            objectInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        datas.addAll(dataTous);
        datas.addAll(dataTiaoMus);
        sort();
        return datas;
    }
    public void sort(){
        Object[] tem = this.datas.toArray();
        Arrays.sort(tem,(e1, e2)->{
            if(((Data)e1).getYear()>((Data)e2).getYear())return -1;
            else if(((Data)e1).getYear()==((Data)e2).getYear()) {
                if(((Data) e1).getMonth()>((Data) e2).getMonth())return -1;
                else if(((Data) e1).getMonth()==((Data) e2).getMonth()){
                    if(((Data) e1).getDay()>((Data) e2).getDay())return -1;
                    else if(((Data) e1).getDay()==((Data) e2).getDay()){
                        if(((Data) e1).getFlag()> ((Data) e2).getFlag() )return -1;
                        else if(((Data) e1).getFlag() == ((Data) e2).getFlag() )return 0;
                        else return 1;
                    }
                    else return 1;
                }
                else return 1;
            }
            else return 1;
        });
        ArrayList<Data> data = new ArrayList<>();
        this.datas.clear();
        for(Object object:tem){
            this.datas.add((Data) object);
        }
    }
    public void saveData(){
        ObjectOutputStream objectOutputStream = null;
        dataTous = new ArrayList<>();
        dataTiaoMus = new ArrayList<>();
        for(Data data:datas){
            if(data.getFlag()==2)
            {
                dataTous.add((DataTou)data);
            }
            else dataTiaoMus.add((DataTiaoMu) data);
        }
        try {
            objectOutputStream = new ObjectOutputStream(context.openFileOutput(DATA_TOU_FILE_NAME,Context.MODE_PRIVATE));
            objectOutputStream.writeObject(dataTous);
        }  catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (objectOutputStream !=null){
                    objectOutputStream.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        try {
            objectOutputStream = new ObjectOutputStream(context.openFileOutput(DATA_TIAO_MU_FILE_NAME,Context.MODE_PRIVATE));
            objectOutputStream.writeObject(dataTiaoMus);
        }  catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (objectOutputStream !=null){
                    objectOutputStream.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
